package flc.ast.activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.os.Bundle;
import android.view.View;

import com.blankj.utilcode.util.ToastUtils;
import com.blankj.utilcode.util.UriUtils;
import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;

import java.io.File;
import java.util.List;

import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.adapter.AlbumChildAdapter;
import flc.ast.adapter.PicAlbumAdapter;
import flc.ast.bean.PicAlbumBean;
import flc.ast.databinding.ActivityAlbumBinding;
import flc.ast.utils.AlbumManager;
import flc.ast.utils.MyMediaLoader;
import io.reactivex.rxjava3.core.ObservableEmitter;
import stark.common.basic.utils.RxUtil;

public class AlbumActivity extends BaseAc<ActivityAlbumBinding> {

    public static String sFilePath;
    private PicAlbumAdapter mPicAlbumAdapter;
    private AlbumManager mInstance;
    private boolean isEdit;
    private boolean hasSelectAll;
    private int totalIndex;


    @Override
    protected int onCreate() {
        return R.layout.activity_album;
    }

    @Override
    public void onBackPressed() {
        if (isEdit) {
            mDataBinding.ivAlbumBack.setVisibility(View.VISIBLE);
            mDataBinding.tvCancel.setVisibility(View.GONE);
            mDataBinding.ivAlbumAdd.setVisibility(View.VISIBLE);
            mDataBinding.llAlbumDeleteView.setVisibility(View.GONE);
            isEdit = false;
            mPicAlbumAdapter.setEdit(isEdit);
            mPicAlbumAdapter.notifyDataSetChanged();
        } else {
            finish();
        }
    }

    @Override
    protected void initView() {
        mDataBinding.ivAlbumBack.setOnClickListener(v -> onBackPressed());

        mDataBinding.ivAlbumEdit.setOnClickListener(this);
        mDataBinding.tvCancel.setOnClickListener(this);
        mDataBinding.ivAlbumAdd.setOnClickListener(this);
        mDataBinding.tvAlbumDelete.setOnClickListener(this);

        mInstance = AlbumManager.getInstance();
        mInstance.addListener(iDataChangeListener);

        mDataBinding.rvAlbumPicList.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false));
        mPicAlbumAdapter = new PicAlbumAdapter();
        mDataBinding.rvAlbumPicList.setAdapter(mPicAlbumAdapter);
        mPicAlbumAdapter.setEdit(isEdit);
        mPicAlbumAdapter.addChildClickViewIds(R.id.tvPicAlbumSel);
        mPicAlbumAdapter.setOnItemClickListener(this);
        mPicAlbumAdapter.setOnItemChildClickListener(this);
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.ivAlbumEdit://编辑
                mDataBinding.ivAlbumBack.setVisibility(View.GONE);
                mDataBinding.tvCancel.setVisibility(View.VISIBLE);
                mDataBinding.ivAlbumAdd.setVisibility(View.GONE);
                mDataBinding.llAlbumDeleteView.setVisibility(View.VISIBLE);
                isEdit = true;
                mPicAlbumAdapter.setEdit(isEdit);
                mPicAlbumAdapter.notifyDataSetChanged();
                break;
            case R.id.tvCancel://取消编辑
                mDataBinding.ivAlbumBack.setVisibility(View.VISIBLE);
                mDataBinding.tvCancel.setVisibility(View.GONE);
                mDataBinding.ivAlbumAdd.setVisibility(View.VISIBLE);
                mDataBinding.llAlbumDeleteView.setVisibility(View.GONE);
                isEdit = false;
                mPicAlbumAdapter.setEdit(isEdit);
                mPicAlbumAdapter.notifyDataSetChanged();
                break;
            case R.id.ivAlbumAdd://添加图片
                SelPicActivity.type = 0;
                startActivity(SelPicActivity.class);
                break;
            case R.id.tvAlbumDelete://删除
                delete();
                break;
        }
    }

    @Override
    protected void onItemClickCallback(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        if (adapter instanceof PicAlbumAdapter) {
            switch (view.getId()) {
                case R.id.tvPicAlbumSel:
                    PicAlbumBean item = mPicAlbumAdapter.getItem(position);
                    item.setSelected(!item.isSelected());
                    for (PicAlbumBean.ClassBean classBean : item.getClassBeanList()) {
                        classBean.setSelected(item.isSelected());
                    }
                    mPicAlbumAdapter.notifyDataSetChanged();

                    if (item.isSelected()) {
                        mInstance.addPicAlbum(item.getClassBeanList());
                    } else {
                        mInstance.deletePicAlbum(item.getClassBeanList());
                    }
                    break;
            }
        } else if (adapter instanceof AlbumChildAdapter) {
            AlbumChildAdapter albumChildAdapter = (AlbumChildAdapter) adapter;
            PicAlbumBean.ClassBean item = albumChildAdapter.getItem(position);
            if (!isEdit) {
                return;
            }
            item.setSelected(!item.isSelected());
            albumChildAdapter.notifyDataSetChanged();

            boolean isSelectAll = true;
            for (PicAlbumBean.ClassBean classBean : albumChildAdapter.getData()) {
                if (!classBean.isSelected()) {
                    isSelectAll = false;
                    break;
                }
            }
            PicAlbumBean picAlbumBean = albumChildAdapter.getPicAlbumBean();
            picAlbumBean.setSelected(isSelectAll);
            mPicAlbumAdapter.notifyDataSetChanged();

            if (item.isSelected()) {
                mInstance.addPicAlbum(item);
            } else {
                mInstance.deletePicAlbum(item);
            }
        }
    }

    @Override
    protected void initData() {
        mDataBinding.tvAlbumTitle.setText(sFilePath);
    }

    @Override
    protected void onResume() {
        super.onResume();
        getData();
    }

    private void getData() {
        RxUtil.create(new RxUtil.Callback<List<PicAlbumBean>>() {
            @Override
            public void doBackground(ObservableEmitter<List<PicAlbumBean>> emitter) {
                List<PicAlbumBean> picAlbumBeans = MyMediaLoader.getInstance().loadPicData(true, sFilePath);
                emitter.onNext(picAlbumBeans);
            }

            @Override
            public void accept(List<PicAlbumBean> picAlbumBeanList) {
                if (picAlbumBeanList == null || picAlbumBeanList.size() == 0) {
                    mDataBinding.tvAlbumNoData.setVisibility(View.VISIBLE);
                    mDataBinding.rvAlbumPicList.setVisibility(View.GONE);
                } else {
                    mDataBinding.tvAlbumNoData.setVisibility(View.GONE);
                    mDataBinding.rvAlbumPicList.setVisibility(View.VISIBLE);
                }

                mPicAlbumAdapter.setList(picAlbumBeanList);
                getTotalCount();
            }
        });
    }

    private void delete() {
        List<PicAlbumBean.ClassBean> picAlbumBeans = mInstance.getPicAlbumBeans();
        if (picAlbumBeans.size() == 0) {
            ToastUtils.showShort(R.string.please_sel_delete);
            return;
        }
        for (int i = 0; i < mPicAlbumAdapter.getData().size(); i++) {
            for (int j = 0; j < mPicAlbumAdapter.getData().get(i).getClassBeanList().size(); j++) {
                if (mPicAlbumAdapter.getData().get(i).getClassBeanList().get(j).isSelected()) {
                    mContext.getContentResolver().delete(UriUtils.file2Uri(new File(
                            mPicAlbumAdapter.getData().get(i).getClassBeanList().get(j).getPath())), null, null);
                    mInstance.deletePicAlbum(mPicAlbumAdapter.getData().get(i).getClassBeanList().get(j));
                    mPicAlbumAdapter.getData().get(i).getClassBeanList().remove(j);
                    j--;
                }
            }
            if (mPicAlbumAdapter.getData().get(i).isSelected()) {
                mPicAlbumAdapter.getData().remove(i);
                i--;
            }
        }
        mPicAlbumAdapter.notifyDataSetChanged();
        getTotalCount();
        if (mPicAlbumAdapter.getData().size() == 0) {
            mDataBinding.tvAlbumNoData.setVisibility(View.VISIBLE);
            mDataBinding.rvAlbumPicList.setVisibility(View.GONE);
        }
        ToastUtils.showShort(R.string.delete_suc);
    }

    private void getTotalCount() {
        totalIndex = 0;
        for (PicAlbumBean picAlbumBean : mPicAlbumAdapter.getData()) {
            totalIndex = picAlbumBean.getClassBeanList().size() + totalIndex;
        }
    }

    private AlbumManager.IDataChangeListener iDataChangeListener = new AlbumManager.IDataChangeListener() {
        @Override
        public void onDataChanged(int currentSize) {
            hasSelectAll = currentSize == totalIndex;
        }
    };

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mInstance != null) {
            mInstance.addListener(iDataChangeListener);
        }
    }

}